<?php
class Page extends MY_Site_Controller{
	
	var $skipParentConstructor = TRUE;
	
	function __construct(){
		parent::__construct();
	}
	
     function viewpage($page = "" ){
     	$this->page = $page;
     	$this->loadSitePage(array());
     }
     
     
     
     function customize($itemId = ""){
     	$this->load->model('admin/Item_model' , 'mItem' ,TRUE);
     	$this->load->model('admin/Item_color_model' , 'mItemColor' ,TRUE);
		$this->load->model('admin/Item_section_model' , 'mItemSection' ,TRUE);
		$this->load->model('admin/Item_section_color_model' , 'mItemSectionColor' ,TRUE);
		$this->load->model('admin/Item_view_model' , 'mItemView' ,TRUE);
		$this->load->model('admin/Item_view_section_coordinates_model' , 'mItemViewSectionCoordinates' ,TRUE);
     	
		
		
     	$this->page = "customize";
     	
     	$data = array();
     	
     	if($itemId != ""){
     		$item = $this->mItem->find($itemId);
     		$data['ITEM'] = $item;
     		//$data[]
     		if($item != null ){
     			$this->mItemView->setOrderBy("obj.id" , "desc");
     			$itemView = $this->mItemView->mainListWithPaginationByField("item_id" , $item['id']);
     			$data['ITEMVIEW'] = $itemView ;
     			
     			//item color in section
				//'ITEMCOLORSINSECTION'
				$data['ITEMSECTIONLIST'] = $this->mItemSection->mainListWithPaginationByField("item_id" , $item['id']);
     			
				forEach($data['ITEMSECTIONLIST'] as $_section){
			 		$sectionColors = $this->mItemSectionColor->getColorListBySection($_section['id']);
			 		//echo $_section['name']."----------".count($section);
			 		$data['ITEMCOLORSFORSPECIFICSECTION_'.$_section['id']] = $sectionColors;
			 		//$data['item_section_color_section_'.$_section['id']] = $section;
			 	
				}
			 	
     			forEach($itemView as $view){
     				if(count($data['ITEMSECTIONLIST']) > 0){
     					forEach($data['ITEMSECTIONLIST'] as $itemsection){
     						$data['ITEMSECTIONCOORDINATES_'.$itemsection['id']] = $this->mItemViewSectionCoordinates->mainListWithPaginationByField("item_section_id" , $itemsection['id']);
     					}
     				}
     				
     				//item in section
     				$data['ITEMSECTIONINVIEW_'.$view['id']] = $this->mItemViewSectionCoordinates->getDistinctSectionByView($view['id']);
					if($data['ITEMSECTIONINVIEW_'.$view['id']] != null){
						forEach($data['ITEMSECTIONINVIEW_'.$view['id']] as $section){
							$colorsinviewandsection = 'ITEMCOLORSINSECTIONVIEW_'.$view['id']."_".$section['id'];
							$data[$colorsinviewandsection] = $this->mItemSectionColor->getColorListBySection( $section['id']);
						}
					}
					
					
					$data['ITEMVIEWCOORDINATES_'.$view['id']] =   $this->mItemViewSectionCoordinates->getCoordinatesListByView( $view['id']);
					
					
     			}
     			//find
     			$FIRSTVIEWID = $this->mItemSection->find($data['ITEMSECTIONLIST'][0]['id']);
     			$data['FIRSTVIEWID'] = $FIRSTVIEWID['item_view_id'];
     			
	     		if($this->input->post("action", TRUE) == "savecustomized"){
	     			$this->page = "savecustomized";
	     			$data = $this->savecustomized($data);
	     		}
	     		
     		    if($this->input->post("action", TRUE) == "proceedtopayment"){
	     			$this->page = "payment";
	     			//save order
	     			$data = $this->saveorder($data);
	     		}
	     		
	     		if($this->input->post("action", TRUE) == "viewcustomizedorder"){
	     			$this->page = "savecustomized";
	     			$data = $this->viewcustomizedorder($data);
	     		}
	     		
	     		$data['currentaction'] = $this->input->post("action", TRUE);
	     		
     			$this->loadSitePage($data);
     		}
     	}else{
     		$this->viewpage();
     	}
     }
	 
     
     function savecustomized($data = array()){
     	if($data != null){
     		$data['TEMP_CUSTOMIZEDCOLORFORSECTION'] = array();
     		$draftcolor = array();
     		foreach($data['ITEMSECTIONLIST'] as $itemsection ){
     			$selectedsectioncolor = $this->input->post("item_section_color_" . $itemsection['id'], TRUE);
     			//echo  "-----(".($this->mItemColor->find($selectedsectioncolor) == null ).")--$selectedsectioncolor";
     			
     			$draftcolor[$itemsection['id']] =  $this->mItemColor->find($selectedsectioncolor);
     		}
     		$data['TEMP_CUSTOMIZEDCOLORFORSECTION'] = $draftcolor;
     	}
     	return $data;
     }
     

     function saveorder($data = array()){
     	   $this->load->model('admin/Orders_model' , 'mOrders' ,TRUE);
     	   $this->load->model('admin/Order_details_model' , 'mOrderDetails' ,TRUE);
     	   $this->load->model('admin/Item_model' , 'mItem' ,TRUE);
     	   
     	   $currentItem = $this->mItem->find(ITEM_ID);
     	   
     	   $orders = array(
	     	   "quantity" => "1",
	     	   "createdate" => "now()",
	     	   "updatedate" => "now()",
	     	   "order_status_id" => "1",
	     	   "price" => $currentItem['price'],
     	       "item_id" => ITEM_ID , 
     	       "client_id" => $this->session->userdata(CLIENT_USER_ID)
     	   );
     	   
     	   $this->mOrders->insert($orders);
     	   $currentOrderId = $this->mOrders->max_id();
     	   
     	   foreach($data['ITEMSECTIONLIST'] as $itemsection ){
     			$selectedsectioncolor = $this->input->post("item_section_color_" . $itemsection['id'], TRUE);
     			$draftcolor[$itemsection['id']] =  $this->mItemColor->find($selectedsectioncolor);
     		
     			if($draftcolor[$itemsection['id']]!=null){
     				$orderDetails = array(
     					"color" => $draftcolor[$itemsection['id']]['value'],
     					"item_section_id" => $itemsection['id'],
     					"orders_id" => $currentOrderId,
     				    "valid" => 1
     				);
     				$this->mOrderDetails->insert($orderDetails);
     			}
    		 
    		 }
    		
    		 $this->load->library('merchant');
			 $this->merchant->load('paypal_express');
			 //$settings = $this->merchant->default_settings();
			 $settings = array(
			    'username' => PAYPAL_API_USERNAME,
			    'password' => PAYPAL_API_PASSWORD,
			    'signature' => PAYPAL_API_SIGNATURE,
			    'test_mode' => PAYPAL_TEST_MODE);
			
			$this->merchant->initialize($settings);
			$params = array(
			    'amount' => 100.00,
				'description' => PAYPAL_DESCRIPTION,
			    'currency' => PAYPAL_CURRENCY,
			    'return_url' => base_url()."/page/payment_result/".$this->mOrderDetails->max_id(),
			    'cancel_url' => base_url()."/page/payment_cancel/"
			);
			
			$response = $this->merchant->purchase($params);
     }
     
     function payment_cancel(){
     	$this->page = "payment-success";
     	$this->loadSitePage($data);
     }
     
     function payment_result($orderId = ""){
     	 $this->load->model('admin/Orders_model' , 'mOrders' ,TRUE);
     	 $this->load->model('admin/Order_details_model' , 'mOrderDetails' ,TRUE);
     	 $this->load->model('admin/Item_model' , 'mItem' ,TRUE);
     	 $currentItem = $this->mItem->find(ITEM_ID);
     	   
     	   
     	$this->load->library('merchant');
		$this->merchant->load('paypal_express');
		$settings = array(
			    'username' => PAYPAL_API_USERNAME,
			    'password' => PAYPAL_API_PASSWORD,
			    'signature' => PAYPAL_API_SIGNATURE,
			    'test_mode' => PAYPAL_TEST_MODE);
		
		$this->merchant->initialize($settings);
		
		$params = array(
			    'amount' => $currentItem['price'],
				'description' => PAYPAL_DESCRIPTION,
			    'currency' => PAYPAL_CURRENCY,
			    'return_url' => base_url()."/page/payment_result/".$this->mOrderDetails->max_id(),
			    'cancel_url' => base_url()."/page/payment_cancel/"
			);
		$response = $this->merchant->purchase_return($params);
		
		//echo $response->status();
		if ($response->status() == Merchant_response::COMPLETE){
			$record = array("id" => $orderId,
			 				"order_status_id" => 2);
			$this->mOrders->update($record);
			$this->page = "payment-success";
		}else{
			$this->page = "payment-failed";
		}
		$data = array();
		$this->loadSitePage($data);
     }
     
     function viewcustomizedorder($data = array()){
     	if($data != null){
     		$data['TEMP_CUSTOMIZEDCOLORFORSECTION'] = array();
     		$draftcolor = array();
     		foreach($data['ITEMSECTIONLIST'] as $itemsection ){
     			$selectedsectioncolor = $this->input->post("item_section_color_" . $itemsection['id'], TRUE);
     			if(count($selectedsectioncolor) != 0){
     				$draftcolor[$itemsection['id']]['id'] = $itemsection['id'];
     				$draftcolor[$itemsection['id']]['value'] =  $selectedsectioncolor;
     			}
     		}
     		$data['TEMP_CUSTOMIZEDCOLORFORSECTION'] = $draftcolor;
     	}
     	return $data;
     }
	
     
     function register(){
     	$this->load->model('admin/Client_model' , 'mClient' ,TRUE);
     	$data = array();
     	$this->page = "registration";
     	
     	
     	$formfields = array( "username", "password", "confirmpassword", "email", "firstname" , "lastname", "gender", "address");
     	$clientDetails = array();
     	forEach($formfields as $field){
     		$this->form_validation->set_value($field);
			$this->form_validation->set_rules($field, humanize($field), "trim|xss_clean|required");
     		$temp = array($field => $this->input->post($field , true));
     		//$POSTED_VALUES[trim($field)]
     		if($field != "confirmpassword")
     			$clientDetails[trim($field)]=  $this->input->post($field , true);
     		//array_push($clientDetails, $temp);
     	}
     	$this->validationResult =  $this->form_validation->run();
     	//echo "---------(".$this->validationResult.")---------------";
     	if($this->validationResult == "" || $this->validationResult){
     		$this->page = "registrationcomplete";
     		$this->mClient->insert($clientDetails);
     	}
     	
     	$this->loadSitePage($data);
     }
     
     
     function registration(){
     	$data = array();
     	$this->page = "registration";
     	$this->loadSitePage($data);
     }
     
	 function notfound(){
		$this->loadSitePage(array());
	 }
	 
	 function logout(){
		 $this->session->sess_destroy();
	 }
	 
}
?>